package cn.jimmie.learn.viewArt.weight

import android.content.Context
import android.graphics.Canvas
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import cn.jimmie.learn.viewArt.R

/**
 * function :
 *
 * <p> Create Time:  2019年01月09日 10:00 </p>
 * <p> @author Jimmie.Qian  </p>
 */
class OverlayerView(context: Context, attrs: AttributeSet) : BaseView(context, attrs) {

    private val layer1 = ContextCompat.getDrawable(context, R.drawable.layer1)!!
    private val layer2 = ContextCompat.getDrawable(context, R.drawable.layer2)!!
    private val layer3 = ContextCompat.getDrawable(context, R.drawable.layer3)!!

    var overlayer = false
        set(value) {
            field = value
            invalidate()
        }


    init {
        val size = w / 3
        layer1.setBounds(0, 0, w, h)
        layer2.setBounds(size, 0, w + size, h)
        layer3.setBounds(size * 2, 0, w + size * 2, h)
    }

    override fun onDraw(canvas: Canvas) {
        if (overlayer) drawOverLayer(canvas)
        else drawOneLayer(canvas)
    }


    private fun drawOverLayer(canvas: Canvas) {
        layer1.draw(canvas)
        layer2.draw(canvas)
        layer3.draw(canvas)
    }

    private fun drawOneLayer(canvas: Canvas) {
        val size = w / 3
        canvas.save()
        canvas.clipRect(0, 0, size, height)
        layer1.draw(canvas)
        canvas.restore()

        canvas.save()
        canvas.clipRect(size, 0, size * 2, height)
        layer2.draw(canvas)
        canvas.restore()

        canvas.save()
        canvas.clipRect(size * 2, 0, size * 3, height)
        layer3.draw(canvas)
        canvas.restore()
    }
}